package com.prp.gwt.client.entry;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.prp.gwt.client.ClientUtility;
import com.prp.gwt.client.ClientConstants;
import com.prp.gwt.client.Host;
import com.prp.gwt.client.widgets.MessageDialogBox;
import com.prp.gwt.client.widgets.entry.ForgetPasswordPanel;

public class EntryClientListener {
	private static EntryClientListener instance;
	private EntryServiceAsync service = GWT.create(EntryService.class);

	public static EntryClientListener getInstance() {
		if (instance == null)
			instance = new EntryClientListener();
		return instance;
	}

	public void signIn(String email, String password) {
		service.signIn(email, password, new AsyncCallback<Integer>() {

			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Integer result) {
				switch (result) {
				case ClientConstants.SIGNIN_USER:
					Window.Location.replace(Host.getHOME());
					break;
				case ClientConstants.SIGNIN_ADMIN:
					Window.Location.replace(Host.getHOME());
					break;
				case ClientConstants.SIGNIN_ROOT:
					Window.Location.replace(Host.getADMIN());
					break;
				case ClientConstants.SIGNIN_NOUSER:
					MessageDialogBox.show("验证错误", "无此用户");
					break;
				case ClientConstants.SIGNIN_PASSERROR:
					MessageDialogBox.show("验证错误", "密码错误");
					break;
				case ClientConstants.SIGNIN_NOTACTIVATED:
					MessageDialogBox.show("验证错误", "用户未激活");
					break;
				}
			}

		});
	}
	
	public void retrievePassword(String email){
		service.retrievePassword(email, new AsyncCallback<Boolean>(){
			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Boolean result) {
				if(result){
					ForgetPasswordPanel.getInstance().showMessage("已经将您的密码发送至您的邮箱!");
				}
				else{
					ForgetPasswordPanel.getInstance().showMessage("用户不存在,请检查邮箱");
				}
			}
		});
	}
}
